package com.dongdong.zhuangji.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dongdong.zhuangji.entity.PileData;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import java.util.List;

@Mapper
public interface PileDataMapper extends BaseMapper<PileData> {
    // 原有的 updatePileData 方法
    @Update("UPDATE pile_data " +
            "SET pile_number = #{pileNumber}, " +
            "mileage = #{mileage}, " +
            "offset = #{offset}, " +
            "coordinate_x = #{coordinateX}, " +
            "coordinate_y = #{coordinateY}, " +
            "pile_bottom_elevation = #{pileBottomElevation}, " +
            "pile_top_elevation = #{pileTopElevation}, " +
            "pile_diameter = #{pileDiameter}, " +
            "pile_length = #{pileLength}, " +
            "steel_cage_length = #{steelCageLength}, " +
            "over_pouring_length = #{overPouringLength}, " +
            "ground_elevation = #{groundElevation}, " +
            "actual_pile_bottom_elevation = #{actualPileBottomElevation}, " +
            "excavation_time = #{excavationTime}, " +
            "hole_opening_time = #{holeOpeningTime}, " +
            "hole_completion_time = #{holeCompletionTime}, " +
            "reinforcement_cage_lowering_time = #{reinforcementCageLoweringTime}, " +
            "conduit_lowering_time = #{conduitLoweringTime}, " +
            "pouring_time = #{pouringTime}, " +
            "test_block_number = #{testBlockNumber}, " +
            "actual_pouring_volume = #{actualPouringVolume}, " +
            "elevation_between_rock_and_soil = #{elevationBetweenRockAndSoil}, " +
            "elevation_casing_pipe = #{elevationCasingPipe}, " +
            "weather = #{weather}, " +
            "actual_steel_cage_length = #{actualSteelCageLength} " +
            "time_for_reinforcement_cage_acceptance = #{timeForReinforcementCageAcceptance} " +
            "time_for_hole_formation_acceptance = #{timeForHoleFormationAcceptance} " +
            "WHERE id = #{id}")
    int updatePileData(PileData pileData);

    /**

    private Date time_for_reinforcement_cage_acceptance;
  time_for_hole_formation_acceptance;
     */
    // 新增：单个修改实测钢筋笼长度的方法
    @Update("UPDATE pile_data " +
            "SET actual_steel_cage_length = #{actualSteelCageLength} " +
            "WHERE pile_number = #{pileNumber}")
    int updateSteelCageLength(
            @Param("pileNumber") String pileNumber,
            @Param("actualSteelCageLength") Double actualSteelCageLength
    );

    // 原有的批量更新方法
    @Update("<script>" +
            "UPDATE pile_data " +
            "SET actual_steel_cage_length = CASE pile_number " +
            "<foreach collection=\"list\" item=\"item\" separator=\"\">" +
            "WHEN #{item.pileNumber} THEN #{item.actualSteelCageLength} " +
            "</foreach>" +
            "END " +
            "WHERE pile_number IN " +
            "<foreach collection=\"list\" item=\"item\" open=\"(\" separator=\",\" close=\")\">" +
            "#{item.pileNumber}" +
            "</foreach>" +
            "</script>")
    int batchUpdateSteelCageLength(@Param("list") List<PileData> pileDataList);

    /**
     * 统计已完成浇筑的桩基数量
     * @return 已完成浇筑的桩基数量
     */
    @Select("SELECT COUNT(*) FROM pile_data WHERE pouring_time IS NOT NULL")
    Integer countCompletedPiles();

    /**
     * 查询指定时间范围内的浇筑记录
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 符合条件的桩基数据列表
     */
    @Select("SELECT * FROM pile_data WHERE pouring_time BETWEEN #{startTime} AND #{endTime}")
    List<PileData> selectByPouringTimeRange(
            @Param("startTime") Date startTime,
            @Param("endTime") Date endTime
    );

    /**
     * 根据里程更新岩土交界高程（单条）
     * @param mileage 里程（需与数据库中存储的精度一致）
     * @param elevation 新的岩土交界高程
     * @return 受影响的行数（0表示未找到匹配记录）
     */
    @Update("UPDATE pile_data " +
            "SET elevation_between_rock_and_soil = #{elevation} " +
            "WHERE ROUND(mileage, 3) = #{mileage}") // 保留3位小数匹配，可根据实际需求调整精度
    int updateElevationByMileage(
            @Param("mileage") Double mileage,
            @Param("elevation") Double elevation
    );
}